package com.ihcy.base.controller;

import com.ihcy.base.config.RedisUtil;
import com.ihcy.base.core.Result;
import com.ihcy.base.service.RedisService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("redis")
public class RedisController {

    private final RedisUtil redisUtil;
    private final RedisService redisService;

    public RedisController(RedisUtil redisUtil, RedisService redisService) {
        this.redisUtil = redisUtil;
        this.redisService = redisService;
    }

    @GetMapping("save")
    public Result<Void> save(String key, String value) {
        redisUtil.set(key, value);
        return Result.ok(null);
    }

    @GetMapping("get")
    public Result<Object> get(String key) {
        return Result.ok(redisUtil.get(key));
    }

    @GetMapping("lock")
    public Result<Boolean> lock(String key, String requestId) {
        return Result.ok(redisService.lock(key, requestId));
    }

    @GetMapping("unlock")
    public Result<Boolean> unlock(String key, String requestId) {
        return Result.ok(redisService.unlock(key, requestId));
    }
}
